//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "ia\fn_sniper.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_grp", "_pos","_radius","_debug","_units","_list","_wp","_bldgpos","_pos_details"];
DEFAULT_PARAM(_grp,0,grpNull);
DEFAULT_PARAM(_pos,1,[]);
DEFAULT_PARAM(_radius,2,100);
DEFAULT_PARAM(_debug,3,false);

if (AND_ALSO((!isnull _grp),IS_OBJECT(_grp))) then {
	_grp = group _grp;
};
if (AND_ALSO((!isnull _grp),IS_GROUP(_grp))) then {
	if (!isServer && !(local (leader _grp))) exitWith { ERROR_1("Execution should be on server or local to the unit",_this); };
	if (!AND_ALSO(IS_ARRAY(_pos),(count _pos == 3))) then {
		_pos = getPos (leader _grp);
	};

	_grp setBehaviour "AWARE";

	_units = (units _grp);
	DEBUG_1("Beginning",ARR_3(_grp,_pos,_units));
	if ((count _pos != 3) || {(([0,0,0] vectorDistance _pos) < 1)} || {count _units <= 0}) exitWith {
		ERROR("Position not set or empty group");
	};

	[_grp] call FFUNC(deleteWaypoints);

	_wp = _grp addWaypoint [_pos, 10];
	_wp setWaypointType "HOLD";
	_grp allowFleeing 0;
	_grp setBehaviour "STEALTH";
	_grp setCombatMode "RED";

	_bldgpos = [_pos, _radius] call FFUNC(buildingSniperPosBuilder);
	
	for "_i" from 0 to ((count _units) min (count _bldgpos)) - 1 do {
		_idx = _bldgpos call FFUNC(arrayRandomIndex);
		if (_idx >= 0) then {
			_pos_details = _bldgpos select _idx;
			_bldgpos set [_idx, "XxDeLxX"];
			_bldgpos = _bldgpos - ["XxDeLxX"];
			(_units select _i) setPosATL (_pos_details select 0);
			(_units select _i) setUnitPosWeak (_pos_details select 2);
			(_units select _i) call FFUNC(stuckUnit2);
			DEBUG_1("Sniper set",ARR_3(_grp,_units select _i,_pos_details));
		};
	};
	
	If (_debug) then {
		_units spawn {
			private ["_group","_units","_marker","_marker_name"];
			sleep 8;
			_units = _this;
			{
				_marker_name = Format["Unit%1",str(_x)];
				_marker = createMarker[_marker_name,[(getPos _x) select 0,(getPos _x) select 1]];
				_marker setMarkerShape "ICON";
				_marker_name setMarkerType "loc_Stack";
				_marker_name setMarkerColor "ColorRed";
			} foreach _units;
		};
	};
} else {
	ERROR("Group is null");
};
TRACE("End");
TMFPROFILERSTOP;